//
//  HMComposeToolBar.swift
//  swift-新浪微博0
//
//  Created by 赵玉忠 on 16/9/9.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit


/// 用于判断按钮类型的枚举
///
/// - picture: 图片
/// - mention: @
/// - trend:   #
/// - emotion: 表情
/// - add:     +
enum ComposeToolBarButtonType: Int {
    //    case picture = 0
    //    case mention = 1
    //    case trend = 2
    //    case emotion = 3
    //    case add = 4
    case picture = 0, mention, trend, emotion, add
}


/// 定义的协议
protocol HMComposeToolBarDelegate: NSObjectProtocol {
    
    /// 定义的协议方法
    func composeToolBar(toolBar: HMComposeToolBar, type: ComposeToolBarButtonType)
}


class HMComposeToolBar: UIView {
    
    // 代理属性
    weak var delegate: HMComposeToolBarDelegate?
    
    
    /// 定义一个属性供外界设置当前是什么键盘，根据不同的键盘显示不同的图标
    var isSystemKeyboard: Bool = true {
        didSet {
            // 更改切换键盘按钮的图标
            let button = self.viewWithTag(3) as! UIButton
            
            // 默认是键盘的图标
            var imgNamed = "compose_keyboardbutton_background"
            // 如果发现是系统键盘，那么更改图标名字为笑脸的
            if isSystemKeyboard {
                imgNamed = "compose_emoticonbutton_background"
            }
            // 设置不同状态的图标
            button.setImage(UIImage(named:imgNamed), for: UIControlState.normal)
            button.setImage(UIImage(named:"\(imgNamed)_highlighted"), for: UIControlState.highlighted)
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        addSubview(stackView)
        
        stackView.snp_makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        
        addChildButton(imgNamed: "compose_toolbar_picture", type: .picture)
        addChildButton(imgNamed: "compose_mentionbutton_background", type: .mention)
        addChildButton(imgNamed: "compose_trendbutton_background", type: .trend)
        addChildButton(imgNamed: "compose_emoticonbutton_background", type: .emotion)
        addChildButton(imgNamed: "compose_add_background", type: .add)
    }
    
    private func addChildButton(imgNamed: String, type: ComposeToolBarButtonType) {
        let button = UIButton()
        
        button.tag = type.rawValue
        // 添加点击事件
        button.addTarget(self, action: #selector(childButtonClick(button:)), for: UIControlEvents.touchUpInside)
        
        button.setImage(UIImage(named: imgNamed), for: UIControlState.normal)
        button.setImage(UIImage(named: "\(imgNamed)_highlighted"), for: UIControlState.highlighted)
        
        stackView.addArrangedSubview(button)
    }
    
    // MARK: - 监听事件
    @objc private func childButtonClick(button: UIButton) {
        
        // 要在这个地方将事件抛给控制器
        // 需要在这个地方执行代理方法，执行闭包，发送通知
        let type = ComposeToolBarButtonType(rawValue: button.tag)!
        delegate?.composeToolBar(toolBar: self, type: type)
    }
    
    // MARK: - 懒加载事件
    
    private lazy var stackView: UIStackView = {
        // FDStackView
        let v = UIStackView()
        // 设置布局方向
        // v.axis =
        v.distribution = .fillEqually
        return v
    }()
    
}
